<?php
namespace plugins\module_config\controller;

use app\common\lib\model_operate\Tools; //引入此类
use app\common\lib\user_operate\ModuleOperate as MO;
use cmf\controller\PluginAdminBaseController;
use plugins\module_config\model\PluginApiIndexModel as Model;

/**
 * 模块关联配置
 */
class RelevanceConfigController extends PluginAdminBaseController
{
    protected function _initialize()
    {
        parent::_initialize();
        $this->Model = new Model();
        $adminId     = cmf_get_current_admin_id(); //获取后台管理员id，可判断是否登录
        if (!empty($adminId)) {
            $this->assign("admin_id", $adminId);
        }
    }
    /**
     * 关联配置
     */
    public function index()
    {
        $data = $this->request->param();
        if (!empty($data['name_storeid'])) {
            $name_storeid     = $data['name_storeid'];
            $data['name']     = explode('|', $name_storeid)[0];
            $data['store_id'] = explode('|', $name_storeid)[1];
        }
        //模块列表
        $mo          = new MO();
        $module_list = $mo->getModuleList();
        if ($module_list['status'] != 'success') {
            $list = [];
        } else {
            $list = $module_list['data'];
        }
        $this->assign('module_list', $list);
        //模块配置文件列表
        $file_list = $this->Model->getModuleTags($data['name']);
        $this->assign('file_list', $file_list);
        //关联方法列表
        $g_meth = new Tools();
        $g_meth = $g_meth->getQueryMethod();
        unset($g_meth['Single']);
        $this->assign('g_meth', $g_meth);
        $data['file_name'] = !empty($data['file_name']) ? $data['file_name'] : '';
        //获取关联配置
        $relevanceType = $this->Model->getModuleTags($data['name'], $data['file_name']);
        if (!empty($relevanceType['table']['relevanceType'])) {
            $relevanceType = $relevanceType['table']['relevanceType'];
        } else {
            $relevanceType = [];
        }
        $this->assign('relevanceType', $relevanceType);
        //控件类型
        $ctrl_type = $this->Model->getTagsConfig();
        $ctrl_type = $ctrl_type['ctrl_type'];
        $this->assign('ctrl_type', $ctrl_type);
        $this->assign('data', $data);
        return $this->fetch();
    }
    /**
     * 获取table关联配置
     */
    public function getRelevance()
    {
        $data = $this->request->param();
        if (!empty($data['name_storeid'])) {
            $name_storeid     = $data['name_storeid'];
            $data['name']     = explode('|', $name_storeid)[0];
            $data['store_id'] = explode('|', $name_storeid)[1];
        }
        if (empty($data['file_name'])) {
            return $this->success('success', '', []);
        }
        $search = $this->Model->getModuleTags($data['name'], $data['file_name'])['table'];
        //获取需要关联的字段
        $ziduan = !empty($search['fields']) ? $search['fields'] : [];
        $list   = array_filter($ziduan, function ($t) {return $t['is_relevance'] == 1;});
        $field = array_keys($list);
        $da    = [
            'field'     => $field,
            'relevance' => !empty($search['relevance']) ? $search['relevance'] : [],
        ];
        return $this->success('success', '', $da);
    }
    /**
     * 保存配置
     */
    public function saveConfig()
    {
        $data = $this->request->param();
        if (!empty($data['name_storeid'])) {
            $name_storeid     = $data['name_storeid'];
            $data['name']     = explode('|', $name_storeid)[0];
            $data['store_id'] = explode('|', $name_storeid)[1];
        }
        if (empty($data['form'])) {
            $this->error('提交内容不能为空');
        }
        $form = $data['form'];
        $form = autoHtmlspecialcharsDecode($form);
        $form = array_column($form, 'relev', 'field');
        //1.取出原来所有的值
        $tags                           = $this->Model->getModuleTags($data['name'], $data['file_name']);
        $tags['table']['relevance']     = $form;
        $tags['table']['relevanceType'] = $data['relevanceType'];
        if (empty($data['file_name'])) {
            $this->error('未选择文件保存位置');
        }
        //写入文件
        $save = $this->Model->saveTags($data['name'], $data['file_name'], $tags);
        if ($save) {
            $this->success('保存成功');
        } else {
            $this->error('保存失败');
        }
    }
}
